# encoding: utf-8
"""
@author: 夏洛
@QQ: 1972386194
@file: 知网识别.py
"""

import ddddocr,requests
import requests
import execjs

ocr = ddddocr.DdddOcr()
session = requests.session()

js_code = """
    new Date().getTime()
"""
tt = execjs.eval(js_code)
def get_code():
    headers2 = {
    "Host": "my.cnki.net",
    "Connection": "keep-alive",
    "Pragma": "no-cache",
    "Cache-Control": "no-cache",
    "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Google Chrome\";v=\"100\"",
    "sec-ch-ua-mobile": "?0",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
    "sec-ch-ua-platform": "\"Windows\"",
    "Accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
    "Sec-Fetch-Site": "same-origin",
    "Sec-Fetch-Mode": "no-cors",
    "Sec-Fetch-Dest": "image",
    "Referer": "https://my.cnki.net/Register/CommonRegister.aspx?platform=toplogin&returnUrl=%2f%2fmy.cnki.net%3fplatform%3dtoplogin",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
    "Cookie": "Ecp_ClientId=2210416223206476211; _pk_ref=%5B%22%22%2C%22%22%2C1650870807%2C%22https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DDlxuNml1u4W2joKAxWeD7yxNe7BOazAz0MoZQy9vOfq%26wd%3D%26eqid%3Db978a51300055b770000000662664a11%22%5D; _pk_ses=*; Ecp_IpLoginFail=220425113.246.155.240; SID_mycnki=020102; ASP.NET_SessionId=drrsu3vnqgznyzybjoclze2n; Ecp_loginuserjf=11327193@qq.com; Ecp_lout=1; Ecp_ClientIp=113.246.155.240; Hm_lvt_3466711a9cd1cb9c9867908f7bc5bdff=1650872645; Hm_lpvt_3466711a9cd1cb9c9867908f7bc5bdff=1650872645; _pk_id=d6c2047a-c7df-46a1-9d76-a0cd6a4b4a31.1618583528.8.1650872645.1650870807."
}
    session.headers = headers2
    url1 = 'https://my.cnki.net/Register/CheckCode.aspx?id={}'.format(tt)
    res = session.get(url1)
    return res.content

def verify_code():
    '''
    效验验证码信息
    :return:
    '''
    session = requests.session()
    url = 'https://my.cnki.net/Register/Server.aspx'
    text = ocr.classification(get_code())
    print(text)
    params = {
    "imageCode": text.upper(),
    # "imageCode": "6HBV",
    "checkFlag": "1",
    "operatetype": "7"
}
    # https://my.cnki.net/Register/Server.aspx?imageCode=26FS&checkFlag=1&operatetype=7
    headers1 = {
    "Host": "my.cnki.net",
    "Connection": "keep-alive",
    "Pragma": "no-cache",
    "Cache-Control": "no-cache",
    "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Google Chrome\";v=\"100\"",
    "Accept": "*/*",
    "X-Requested-With": "XMLHttpRequest",
    "sec-ch-ua-mobile": "?0",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
    "sec-ch-ua-platform": "\"Windows\"",
    "Sec-Fetch-Site": "same-origin",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Dest": "empty",
    "Referer": "https://my.cnki.net/Register/CommonRegister.aspx?platform=toplogin&returnUrl=%2f%2fmy.cnki.net%3fplatform%3dtoplogin",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
    "Cookie": "Ecp_ClientId=2210416223206476211; _pk_ref=%5B%22%22%2C%22%22%2C1650870807%2C%22https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DDlxuNml1u4W2joKAxWeD7yxNe7BOazAz0MoZQy9vOfq%26wd%3D%26eqid%3Db978a51300055b770000000662664a11%22%5D; _pk_ses=*; Ecp_IpLoginFail=220425113.246.155.240; SID_mycnki=020102; ASP.NET_SessionId=drrsu3vnqgznyzybjoclze2n; Ecp_loginuserjf=11327193@qq.com; Ecp_lout=1; Ecp_ClientIp=113.246.155.240; Hm_lvt_3466711a9cd1cb9c9867908f7bc5bdff=1650872645; Hm_lpvt_3466711a9cd1cb9c9867908f7bc5bdff=1650872645; _pk_id=d6c2047a-c7df-46a1-9d76-a0cd6a4b4a31.1618583528.8.1650872645.1650870807."
}
    session.headers = headers1
    res = session.get(url,params=params,headers=headers1,verify=False)
    print(res.text)
    return res.text
    # 返回1 表示验证码突破成功
verify_code()

